package com.dexilaonian.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.ServiceDetailAdaprer;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


public class AgingDetailActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.aging_shilao)
    TextView agingShilao;
    @Bind(R.id.aging_shilao_money)
    TextView agingShilaoMoney;
    @Bind(R.id.tv_aging_service)
    TextView tvAgingService;
    @Bind(R.id.iv_aging_service)
    View ivAgingtService;
    @Bind(R.id.tv_aging_imfor)
    TextView tvAgingImfor;
    @Bind(R.id.iv_aging_imfor)
    View ivAgingImfor;
    @Bind(R.id.aging_ViewPager)
    ViewPager agingViewPager;
    @Bind(R.id.btn_aging_door)
    Button btnAgingDoor;
    private String aging_detail_url;
    private String mIDD2;
    private List<View> DetailList = new ArrayList<>();
    private ServiceDetailAdaprer list_Detail;
    private View mAgingContenterVice;
    private View mAging_order;
    private String projectid;
    private String introduce;
    private String contentTime;
    private String name;
    private String originalPrice;

    @Override
    public int getLayoutID() {
        return R.layout.activity_aging_detail;
    }
    @Override
    public void initView() {
        aging_detail_url = Constant.base_url + "/product/getDetailOfProduct";
    }
    @Override
    protected void initData() {
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        Intent intent = getIntent();
        Bundle data   = intent.getExtras();
        mIDD2 = String.valueOf(data.getInt("IDD2"));
        init();
        initGetDetail();
    }

    @OnClick({R.id.back, R.id.tv_aging_service, R.id.tv_aging_imfor, R.id.btn_aging_door})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_aging_service:
                agingViewPager.setCurrentItem(0);
                tvAgingService.setTextColor(Color.parseColor("#009a99"));
                tvAgingImfor.setTextColor(Color.parseColor("#333333"));
                ivAgingtService.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
                ivAgingImfor.setBackground(null);
                break;
            case R.id.tv_aging_imfor:
                agingViewPager.setCurrentItem(1);
                tvAgingImfor.setTextColor(Color.parseColor("#009a99"));
                tvAgingService.setTextColor(Color.parseColor("#333333"));
                ivAgingImfor.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
                ivAgingtService.setBackground(null);
                break;
            case R.id.btn_aging_door:
                Intent intent = new Intent(AgingDetailActivity.this, AgingConfiActivity.class);
                intent.putExtra("INTRODUCE",introduce);
                intent.putExtra("originalPrice",originalPrice);
                intent.putExtra("ContentTime",contentTime);
                intent.putExtra("NAME",name);
                intent.putExtra("projectid", projectid);
                Bundle bundle = new Bundle();
                //bundle.putInt("projectid", projectid);

                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }
    private void initGetDetail() {
        Map map = new HashMap<>();
        map.put("productid", mIDD2);
        Okhttp.postString(false, aging_detail_url, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                Log.e("TTTageingDetail", response);
                try {
                    JSONObject object = new JSONObject(response);
                    int status = Integer.parseInt(object.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(AgingDetailActivity.this, LoginActivity.class));
                        return;
                    }
                    if (status == 0) {
                        String data = object.optString("data");
                        JSONObject jsonObject = new JSONObject(data);
                        originalPrice = jsonObject.optString("original_price");
                        introduce = jsonObject.optString("introduce");
                        contentTime = jsonObject.optString("content_time");
                        name = jsonObject.optString("name");
                        projectid = String.valueOf(jsonObject.optInt("id"));
                        String suggest = jsonObject.optString("suggest");
                        String content = jsonObject.optString("content");

                        if (!TextUtils.isEmpty(originalPrice)) {
                            agingShilaoMoney.setText("￥"+ originalPrice);
                        }
                        if (!TextUtils.isEmpty(name)) {
                            agingShilao.setText(name);
                        }
                        TextView agingtext = (TextView) mAgingContenterVice.findViewById(R.id.tv_content_view);
                        TextView informationtext = (TextView) mAging_order.findViewById(R.id.tv_order_view);
                        if (!TextUtils.isEmpty(content)) {
                            agingtext.setText(content);
                        }
                        if (!TextUtils.isEmpty(suggest)) {
                            informationtext.setText(suggest);
                        }
                        DetailList.add(mAgingContenterVice);
                        DetailList.add(mAging_order);
                        list_Detail = new ServiceDetailAdaprer(AgingDetailActivity.this, DetailList);
                        agingViewPager.setAdapter(list_Detail);
                        list_Detail.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {

            }
        });
    }
    private void init() {
        mAgingContenterVice = View.inflate(AgingDetailActivity.this, R.layout.fragment_contentervice,
                null);

        mAging_order = View.inflate(AgingDetailActivity.this,
                R.layout.fragment_order,
                null);

        tvAgingService.setOnClickListener(this);
        tvAgingImfor.setOnClickListener(this);

        agingViewPager.setCurrentItem(0);
        agingViewPager.setOffscreenPageLimit(3);
        agingViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                chanceColorText(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void chanceColorText(int position) {
        if (position == 0) {
            tvAgingService.setTextColor(Color.parseColor("#009a99"));
            tvAgingImfor.setTextColor(Color.parseColor("#333333"));
            ivAgingtService.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
            ivAgingImfor.setBackground(null);

        } else if (position == 1) {
            tvAgingImfor.setTextColor(Color.parseColor("#009a99"));
            tvAgingService.setTextColor(Color.parseColor("#333333"));
            ivAgingImfor.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
            ivAgingtService.setBackground(null);
        }
    }
}
